#include "suppliercontroller.h"
#include "core/suppliermodel.h"
#include <QDebug>
#include <QSqlQuery>
#include <QSqlError>

SupplierController::SupplierController(QObject *parent) :
    BaseController(parent)
{
}

void SupplierController::updateSupplier(SupplierModel *supplier)
{
    QString sql;
    QSqlQuery query;

    sql.append("UPDATE Suppliers "
               "SET name=:name, "
               "contactNumber=:contactNumber, "
               "country=:country "
               "WHERE ID=:id");
    query.prepare(sql);
    query.bindValue(":name", supplier->getName());
    query.bindValue(":contactNumber", supplier->getContactNumber());
    query.bindValue(":country", supplier->getCountry());
    query.bindValue(":id",supplier->getID());
    qDebug() << "Updating supplier with ID " << supplier->getID();

    bool ok = query.exec();

    if(!ok) {
        qDebug() << "SupplierController::updateSupplier Query failed!" << query.lastError().text();
    }
}

void SupplierController::addSupplier(SupplierModel *supplier)
{
    QString sql;
    QSqlQuery query;

    sql.append("INSERT INTO Suppliers "
               "(name, contactNumber, country) "
               "values (:name, :contactNumber, :country)");
    query.prepare(sql);
    query.bindValue(":name", supplier->getName());
    query.bindValue(":contactNumber", supplier->getContactNumber());
    query.bindValue(":country", supplier->getCountry());

    bool ok = query.exec();

    if(!ok) {
        qDebug() << "SupplierController::addSupplier Query failed!" << query.lastError().text();
    }
}

QList<BaseModel *> SupplierController::getAll() {
    QList<BaseModel *> list;
    SupplierModel *supplier;
    QSqlQuery query;

    bool ok = query.exec("SELECT ID, name, contactNumber, country FROM Suppliers");

    if (!ok) {
        qDebug() << "SupplierController: Query Failed!"
                 << query.lastError().text();
    }

    while (query.next()) {
        supplier = new SupplierModel();
        supplier->setID(query.value(0).toInt());

        supplier->setName(query.value(1).toString());
        supplier->setContactNumber(query.value(2).toString());
        supplier->setCountry(query.value(3).toString());

        list.append(supplier);
    }

    emit update(list);
    return list;
}

SupplierModel *SupplierController::getSupplierByName(QString supplierName) {
    QString sql;
    QSqlQuery query;
    bool ok;
    SupplierModel *supplier;

    sql = "SELECT ID, contactNumber, country FROM Suppliers "
            "WHERE name = :name ";
    query.prepare(sql);
    query.bindValue(":name", supplierName);

    ok = query.exec();

    if (!ok) {
        qDebug() << "SupplierController::getSupplierByName Query Failed!" << query.lastError().text();
    } else {
        if (!query.next()) {
            return NULL;
        }

        supplier = new SupplierModel;

        supplier->setID(query.value(0).toInt());
        supplier->setContactNumber(query.value(1).toString());
        supplier->setCountry(query.value(2).toString());
        supplier->setName(supplierName);

        return supplier;
   }
}
